#include <QMessageBox>
#include <QColorDialog>
#include <QFileDialog>

#include "newmapdialog.h"
#include "ui_newmapdialog.h"

NewMapDialog::NewMapDialog(QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::NewMapDialog)
{
    m_ui->setupUi(this);

    colour = new QColor(255, 0, 255);

    QPalette palette = m_ui->colourDemoFrame->palette();
    palette.setColor(backgroundRole(), *colour);
    m_ui->colourDemoFrame->setPalette(palette);
    m_ui->colourDemoFrame->setAutoFillBackground(true);


    connect(m_ui->changeColourButton, SIGNAL(clicked()), this, SLOT(onChangeColourClicked()));
    connect(m_ui->tileSetBrowseButton, SIGNAL(clicked()), this, SLOT(onBrowseTileSetClicked()));
}

NewMapDialog::~NewMapDialog()
{
    if (colour != 0)
    {
        delete colour;
    }

    delete m_ui;
}

void NewMapDialog::onChangeColourClicked()
{
    QColorDialog dialogue(this);
    if (dialogue.exec() == QDialog::Accepted)
    {
        *colour = dialogue.currentColor();
        QPalette palette = m_ui->colourDemoFrame->palette();
        palette.setColor(backgroundRole(), *colour);
        m_ui->colourDemoFrame->setPalette(palette);
        m_ui->colourDemoFrame->setAutoFillBackground(true);
    }
}

void NewMapDialog::onBrowseTileSetClicked()
{
    QString filename = QFileDialog::getOpenFileName(this, "Open tile set","./tilesets/", "Images (*.png *.gif *.bmp *.jpg)");
    if (filename != "")
    {
        m_ui->tileSetFileName->setText(filename);
    }
}

void NewMapDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

qint32 NewMapDialog::getTileSizeX()
{
    return m_ui->tileSizeXComboBox->currentText().toInt();
}

qint32 NewMapDialog::getTileSizeY()
{
    return m_ui->tileSizeYComboBox->currentText().toInt();
}

qint32 NewMapDialog::getMapSizeX()
{
    return m_ui->mapSizeXSpinBox->value();
}

qint32 NewMapDialog::getMapSizeY()
{
    return m_ui->mapSizeYSpinBox->value();
}

QString NewMapDialog::getTileSetPath()
{
    return m_ui->tileSetFileName->text();
}
